package com.matrixxun.starry.badgetextview;

import com.matrixxun.starry.badgetextview.util.AttrUtil;
import com.matrixxun.starry.badgetextview.util.ShapeUtil;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.logging.Level;
import java.util.logging.Logger;

public class MenuItemBadge extends StackLayout {
    private final String TXT_BG_COLOR = "txt_bg_color";
    private final String TXT_COLOR = "txt_color";
    private final String IMG_RES_ID = "img_res_id";
    private Color txtBgColor;
    private Color txtColor;
    private int imgResId;
    private String title;
    private Image image;
    private MaterialBadgeTextView materialBadgeTextView;
    private Component blankView;
    private AttrUtil attrUtils;

    public MenuItemBadge(Context context) {
        this(context, null);
    }

    public MenuItemBadge(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public MenuItemBadge(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet);
    }

    private void init(Context context, AttrSet attrSet) {
        attrUtils = new AttrUtil(attrSet);
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_menu_action_item_badge, this, true);
        image = (Image) findComponentById(ResourceTable.Id_menu_badge_icon);
        materialBadgeTextView = (MaterialBadgeTextView) findComponentById(ResourceTable.Id_menu_badge);
        blankView = findComponentById(ResourceTable.Id_menu_no_data);
        blankView.setVisibility(Component.HIDE);
        txtBgColor = attrUtils.getAttrColor(TXT_BG_COLOR, Color.TRANSPARENT);
        txtColor = attrUtils.getAttrColor(TXT_COLOR, Color.TRANSPARENT);
        imgResId = attrUtils.getAttrOther(IMG_RES_ID, ResourceTable.Media_icon);
        setTxtBgColor(txtBgColor);
        setTxtColor(txtColor);
        setImgRes(imgResId);
    }

    private void setTxtBgColor(Color color) {
        if (color != Color.TRANSPARENT) {
            materialBadgeTextView.setBackgroundColor(txtBgColor);
        }
    }

    private void setTxtColor(Color color) {
        if (color != Color.TRANSPARENT) {
            materialBadgeTextView.setTextColor(color);
        }
    }

    private void setImgRes(int resId) {
        if (resId > 0) {
            image.setPixelMap(resId);
        }
    }

    /**
     * 非数值型数据时显示文字
     *
     * @param txt 字符串型文字
     */
    public void setBadgeCount(String txt) {
        if ("".equals(txt)) {
            showDot(true);
            return;
        }
        showDot(false);
        int temp = -1;
        try {
            temp = Integer.parseInt(txt);
        } catch (Exception e) {
            Logger.getLogger(MenuItemBadge.class.getName()).log(Level.SEVERE, e.getMessage());
        }
        if (temp != -1) {
            setBadgeCount(temp);
        } else {
            materialBadgeTextView.setText(txt);
        }
    }

    /**
     * 显示int型数据
     *
     * @param count 显示角标数字
     */
    public void setBadgeCount(int count) {
        showDot(count < 0);
        materialBadgeTextView.setBadgeCount(count);
    }

    /**
     * 红点和显示文字互拆
     *
     * @param isVisible 是否显示红点
     */
    private void showDot(boolean isVisible) {
        if (isVisible) {
            blankView.setVisibility(Component.VISIBLE);
            materialBadgeTextView.setVisibility(Component.INVISIBLE);
            blankView.setBackground(ShapeUtil.createCircleDrawable(txtBgColor.getValue(), blankView.getHeight() / 2));
        } else {
            blankView.setVisibility(Component.HIDE);
            materialBadgeTextView.setVisibility(Component.VISIBLE);
        }
    }

    /**
     * 明确的展现一个无任何文本的红色圆点
     */
    public void setHighLightMode() {
        showDot(true);
    }

    /**
     * 清除文字及红色圆点
     */
    public void clearHighLightMode() {
        showDot(false);
        materialBadgeTextView.setVisibility(Component.INVISIBLE);
    }
}
